package Servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import DaosImpl.ClientDaoImpl;

/**
 * Servlet implementation class AddMyInfoServlet
 */
@WebServlet("/AddMyInfoServlet")
public class AddMyInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddMyInfoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		String account = (String)session.getAttribute("account");
		String name = request.getParameter("name");
		String idcard = request.getParameter("idcard");
		String age = request.getParameter("age");
		String phone = request.getParameter("phone");
		String sex = request.getParameter("sex");
		String blackList = request.getParameter("blackList");
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
		ApplicationContext context = new FileSystemXmlApplicationContext("D:\\MyEcilipseWorkSpace\\HotelSystem\\src\\applicationContext.xml");
		ClientDaoImpl clientDao = (ClientDaoImpl)context.getBean("clientDao");
		String insSql = "INSERT INTO Client (client_id,client_name,client_phone,client_sex,client_age,client_blacklist) VALUES("+"'"+idcard+"'"+",'"+name+"','"+phone+"','"+sex+"','"+age+"','"+blackList+"');";
		String updateSql="UPDATE Role_Info SET client_id='"+idcard+"' WHERE account='"+account+"';";
		try{
			int i = clientDao.insertInfo(insSql);
			int j = clientDao.insertInfo(updateSql);
		}catch(Exception e){
			e.printStackTrace();
		}
		response.sendRedirect("/HotelSystem/realTimeRoom.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
